home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SAMPLES.BIN / ScrollText.java < prev    next >
Encoding:
Java Source  |  1996-12-14  |  4.5 KB  |  193 lines

  1. import java.awt.*;
  2.  
  3. class ScrollText extends Canvas implements Runnable
  4. {
  5.     Thread scrollThread = null;
  6.     String text;                                //text to scroll
  7.     int scrollUnit = 10;                        //the incremental scrolling distance in pixels
  8.     int sleepTime = 150;                        //controls the speed of the scroll
  9.  
  10.     public static final boolean SCROLL_LEFT = true;
  11.     public static final boolean SCROLL_RIGHT = false;
  12.  
  13.     boolean scrollDirection = SCROLL_LEFT;
  14.     
  15.     private boolean suspended = false;
  16.  
  17.     private int textX;
  18.     private int textY;
  19.     private int textW;
  20.     
  21.     public ScrollText()
  22.     {
  23.         super();
  24.         setText("Visual Cafe from Symantec");
  25.         setBackground(Color.lightGray);
  26.         setForeground(Color.white);
  27.     }
  28.     
  29.     public synchronized void addNotify()
  30.     {
  31.         super.addNotify();
  32.         scrollThread = new Thread(this);
  33.         scrollThread.setPriority(Thread.MIN_PRIORITY);
  34.         scrollThread.start();
  35.     }
  36.  
  37.     public synchronized void removeNotify() {
  38.         if (scrollThread != null) {
  39.             scrollThread.stop();
  40.             scrollThread = null;
  41.         }
  42.         super.removeNotify();
  43.     }
  44.  
  45.     public void startScrollText() {
  46.         suspended = false;
  47.         show();
  48.     }
  49.  
  50.     public void stopScrollText() {
  51.         suspended = true;
  52.     }
  53.  
  54.     public void setText(String str)
  55.     {
  56.         text = str;
  57.         createTextParams();
  58.     }
  59.  
  60.     public String getText()
  61.     {
  62.         return text;
  63.     }
  64.  
  65.     public void setScrollInterval(int speed)
  66.     {
  67.         if (speed < 1)
  68.             sleepTime = 1;
  69.         else 
  70.             sleepTime = speed;
  71.     }
  72.     
  73.     public int getScrollInterval()
  74.     {
  75.         return sleepTime;
  76.     }
  77.     
  78.     public void setScrollUnit(int unit)
  79.     {
  80.         if (unit < 1)
  81.             scrollUnit = 1;
  82.         else
  83.             scrollUnit = unit;
  84.     }
  85.     
  86.     public int getScrollUnit()
  87.     {
  88.         return (scrollUnit < 0) ? -scrollUnit : scrollUnit;
  89.     }
  90.     
  91.     public void setScrollDirection(boolean dir)
  92.     {
  93.         scrollDirection = dir;
  94.     }
  95.  
  96.     public boolean getScrollDirection()
  97.     {
  98.         return scrollDirection;
  99.     }
  100.     
  101.     public synchronized void show() {
  102.         super.show();
  103.         if (isVisible()) {
  104.             if (scrollThread != null) {
  105.                 scrollThread.setPriority(Thread.MAX_PRIORITY);
  106.                 scrollThread.resume();
  107.             }
  108.         }
  109.     }
  110.  
  111.     public synchronized void hide() {
  112.          super.hide();
  113.          if (!isVisible()) {
  114.              if (scrollThread != null)
  115.                 scrollThread.suspend();
  116.          }
  117.      }
  118.  
  119.     public void run()
  120.     {
  121.         createTextParams();
  122.         while (true)
  123.         {
  124.             if (!suspended)
  125.             {
  126.                 nextPos();
  127.                 try
  128.                 {
  129.                     System.gc();
  130.                     Thread.sleep(sleepTime);
  131.                 }
  132.                 catch(Exception e)
  133.                 {
  134.                 }
  135.             }
  136.         }
  137.     }
  138.  
  139.     public synchronized void nextPos()
  140.     {
  141.         Dimension dim = size();
  142.         if (scrollDirection == SCROLL_LEFT)
  143.         {
  144.               textX -= scrollUnit;         //scrollUnit is the incremental distance of scrolling
  145.               if((textX + textW) < 0)
  146.                    textX = dim.width;
  147.         }
  148.         else
  149.         {
  150.             textX += scrollUnit;
  151.             if (textX > dim.width)
  152.                 textX = -textW;
  153.         }
  154.  
  155.         repaint();
  156.     }
  157.     
  158.     private void createTextParams() 
  159.     {
  160.         Font f = getFont();
  161.         if (f == null)
  162.             return;
  163.         FontMetrics fm = getFontMetrics(f);
  164.         int fh = fm.getHeight();
  165.         
  166.         Dimension dim = size();
  167.         textX = dim.width;
  168.         textY = ((dim.height - fh) >> 1) + fm.getAscent();
  169.         textW = fm.stringWidth(text);
  170.     }
  171.     
  172.     public void paint(Graphics g)
  173.     {
  174.         Dimension dim = size();
  175.         Image textImage = createImage(dim.width, dim.height);
  176.         Graphics textGC = textImage.getGraphics();
  177.         
  178.         //draw background rectangle...
  179.         textGC.setColor(getBackground());
  180.         textGC.fillRect(0, 0, dim.width-1, dim.height-1);
  181.  
  182.         //...then draw shadow copy of text...
  183.         textGC.setColor(Color.black);
  184.         textGC.drawString(text, textX+2, textY+2);
  185.  
  186.         //...then draw top copy of text...
  187.         textGC.setColor(getForeground());
  188.         textGC.drawString(text, textX, textY);
  189.         g.drawImage(textImage, 0, 0, this);
  190.     }
  191. }
  192.  
  193.